import org.junit.Test;

import java.sql.*;

public class javaLean1 {
    public void sdd() throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                "lhcx?useSSL=false&serverTimezone=UTC", "root", "123456");
        //定义语句
        String sql="insert into bixiuke values (null,'Java','3.4')";
        //创建数据库操作对象
        Statement statement = connection.createStatement();
        //执行sql,获取结果集
        int i = statement.executeUpdate(sql);
        System.out.println("数据库添加了 " + i+"数据");
        //关闭资源
        statement.close();
        connection.close();
    }
    public void add() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                "lhcx?useSSL=false&serverTimezone=UTC", "root", "123456");
        String sql="insert into major values (null,'信息管理系统')";
        Statement statement = connection.createStatement();
        int i = statement.executeUpdate(sql);
        System.out.println("数据库添加了 " + i+"数据");
        statement.close();
        connection.close();
    }
    public void update() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                "lhcx?useSSL=false&serverTimezone=UTC", "root", "123456");
        String sql="update bixiuke set course='c++' where id='1002'";
        Statement statement = connection.createStatement();
        int i = statement.executeUpdate(sql);
        System.out.println("数据库修改了 " + i+"数据");
        statement.close();
        connection.close();
    }
    public void delete() throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                "lhcx?useSSL=false&serverTimezone=UTC", "root", "123456");
        //定义语句
        String sql="delete from bixiuke where id='1001'";
        //创建数据库操作对象
        Statement statement = connection.createStatement();
        //执行sql,获取结果集
        int i = statement.executeUpdate(sql);
        System.out.println("数据库删除了 " + i+"数据");
        //关闭资源
        statement.close();
        connection.close();
    }

@Test
    public void select() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                "lhcx?useSSL=false&serverTimezone=UTC", "root", "123456");
        String sql = "select * from bixiuke";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println("id:" + resultSet.getInt("id"));
            System.out.println("course:" + resultSet.getString("course"));
            System.out.println("score: " + resultSet.getDouble("score"));
        }
        statement.close();
        connection.close();
    }
}
